package com.cloudcreativity.blyg.ui.provider;

import android.app.Activity;
import android.databinding.ObservableField;
import android.os.Handler;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.base.BaseBindingRecyclerViewAdapter;
import com.cloudcreativity.blyg.base.BaseDialogImpl;
import com.cloudcreativity.blyg.base.BaseModel;
import com.cloudcreativity.blyg.databinding.ActivityProviderListBinding;
import com.cloudcreativity.blyg.databinding.ItemLayoutProviderBinding;
import com.cloudcreativity.blyg.sqlite.Provider;
import com.cloudcreativity.blyg.sqlite.utils.ProviderDaoUtils;
import com.cloudcreativity.blyg.utils.AddProviderUtils;
import com.cloudcreativity.blyg.utils.AlertDialogUtils;
import com.cloudcreativity.blyg.utils.EditProviderUtils;

import java.util.List;

public class ProviderListModel extends BaseModel<Activity, ActivityProviderListBinding> {

    public ObservableField<String> key = new ObservableField<>();

    public BaseBindingRecyclerViewAdapter<Provider, ItemLayoutProviderBinding> adapter;
    private ProviderDaoUtils providerDaoUtils;

    ProviderListModel(Activity context, ActivityProviderListBinding binding, BaseDialogImpl baseDialog) {
        super(context, binding, baseDialog);
        providerDaoUtils = new ProviderDaoUtils(context);
        adapter = new BaseBindingRecyclerViewAdapter<Provider, ItemLayoutProviderBinding>(context) {
            @Override
            protected int getLayoutResId(int viewType) {
                return R.layout.item_layout_provider;
            }

            @Override
            protected void onBindItem(ItemLayoutProviderBinding binding,final Provider item, int position) {
                binding.setItem(item);
                binding.tvDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        AlertDialogUtils utils = new AlertDialogUtils(context,"提示",
                                "确定删除这条供货商记录吗？");
                        utils.setOnResultListener(new AlertDialogUtils.OnResultListener() {
                            @Override
                            public void onOk() {
                                ProviderDaoUtils daoUtils = new ProviderDaoUtils(context);
                                daoUtils.delete(item.getId());
                                refreshData();
                            }

                            @Override
                            public void onCancel() {

                            }
                        });
                        utils.show();
                    }
                });

                binding.tvEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        EditProviderUtils editProviderUtils = new EditProviderUtils(context, item);
                        editProviderUtils.setOnOkListener(new EditProviderUtils.OnOkListener() {
                            @Override
                            public void onOk(Provider provider) {
                                providerDaoUtils.update(provider);
                                refreshData();
                            }
                        });
                        editProviderUtils.show();
                    }
                });
            }
        };
        binding.rcvProvider.setLayoutManager(new LinearLayoutManager(context,
                LinearLayoutManager.VERTICAL,
                false));
        //加上分割线
        DividerItemDecoration itemDecoration = new DividerItemDecoration(context,DividerItemDecoration.VERTICAL);
        itemDecoration.setDrawable(context.getResources().getDrawable(R.drawable.shape_item_divide));
        binding.rcvProvider.addItemDecoration(itemDecoration);
        refreshData();

        binding.etKey.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void afterTextChanged(final Editable editable) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        onSearch(editable.toString());
                    }
                },300);
            }
        });
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initData() {

    }

    public void onBack(){
        context.finish();
    }

    private void onSearch(String key){
        List<Provider> providers = providerDaoUtils
                .queryList(TextUtils.isEmpty(key)?"":key);
        adapter.getItems().clear();
        adapter.getItems().addAll(providers);
    }

    private void refreshData(){
        List<Provider> providers = new ProviderDaoUtils(context).queryList("");
        adapter.getItems().clear();
        adapter.getItems().addAll(providers);
    }

    public void onAddClick(){
        AddProviderUtils addProviderUtils = new AddProviderUtils(context);
        addProviderUtils.show();
        addProviderUtils.setOnOkListener(new AddProviderUtils.OnOkListener() {
            @Override
            public void onOk(Provider provider) {
                new ProviderDaoUtils(context).insert(provider);
                refreshData();
            }
        });
    }
}
